package org.zjt.demo.common;

import org.apache.log4j.Logger;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Created by zhang on 2017/3/2.
 */
public class SessionCounterListener implements HttpSessionListener {
    private static final Logger LOGGER =Logger.getLogger(SessionCounterListener.class);
    private static AtomicLong activeSessions = new AtomicLong(0);


    /* Session创建事件 */
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        long active=activeSessions.incrementAndGet();
        LOGGER.info(String.format("新用户上线！当前在线%s人",active));
    }


    /* Session失效事件 */
    @Override
    public void sessionDestroyed(HttpSessionEvent se) {

        long active=activeSessions.decrementAndGet();
        LOGGER.info(String.format("用户下线！当前在线%s人",active));
    }

}